package com.cheer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.common.Page;
import com.cheer.dao.OaMatrixClassDao;
import com.cheer.dao.OaMatrixDao;
import com.cheer.dto.OaMatrixClassAddDTO;
import com.cheer.dto.OaMatrixClassUpdateDTO;
import com.cheer.entity.OaMatrixClassEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.OaMatrixClassService;
import com.cheer.service.OaMatrixService;
import com.cheer.vo.OaMatrixClassInfoVO;
import com.cheer.vo.OaMatrixClassListVO;
import com.cheer.vo.SimpleObjectVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service("oaMatrixClassService")
public class OaMatrixClassServiceImpl extends ServiceImpl<OaMatrixClassDao, OaMatrixClassEntity> implements OaMatrixClassService {

    @Autowired
    private OaMatrixClassDao oaMatrixClassDao;

    @Autowired
    private OaMatrixService oaMatrixService;

    @Override
    public void add(OaMatrixClassAddDTO matrixClassAddDTO) {
        if (matrixClassAddDTO.getPid() != 0 && oaMatrixClassDao.selectById(matrixClassAddDTO.getPid()) == null) {
            throw new ServiceException("所属分类不存在");
        }

        OaMatrixClassEntity matrixClass = new OaMatrixClassEntity()
                .setPid(matrixClassAddDTO.getPid())
                .setName(matrixClassAddDTO.getName())
                .setAddTime(String.valueOf(DateUtil.currentSeconds()));

        oaMatrixClassDao.insert(matrixClass);
    }

    @Override
    public List<SimpleObjectVO> sub() {
        List<OaMatrixClassEntity> matrixClassList = oaMatrixClassDao.selectListByPid(0);

        List<SimpleObjectVO> simpleObjectVOList = new ArrayList<>();
        simpleObjectVOList.add(new SimpleObjectVO()
                .setId((long) 0)
                .setName("顶级分类"));

        for (OaMatrixClassEntity matrixClass : matrixClassList) {
            simpleObjectVOList.add(new SimpleObjectVO()
                    .setId((long) matrixClass.getId())
                    .setName(matrixClass.getName()));
        }

        return simpleObjectVOList;
    }

    @Override
    public Page<OaMatrixClassListVO> list(Integer pageNum, Integer pageSize) {
        IPage<OaMatrixClassEntity> page = oaMatrixClassDao.selectPage(pageNum, pageSize);

        Set<Integer> pidSet = page.getRecords().stream()
                .map(OaMatrixClassEntity::getPid)
                .collect(Collectors.toSet());
        pidSet.remove(0);

        Map<Integer, String> nameMap = new HashMap<>();
        nameMap.put(0, "顶级分类");
        if (CollectionUtils.isNotEmpty(pidSet)) {
            oaMatrixClassDao.selectBatchIds(pidSet)
                    .forEach(matrixClass -> nameMap.put(matrixClass.getId(), matrixClass.getName()));
        }

        List<OaMatrixClassListVO> list = page.getRecords().stream()
                .map(matrixClass -> new OaMatrixClassListVO()
                        .setId(matrixClass.getId())
                        .setName(matrixClass.getName())
                        .setPname(nameMap.get(matrixClass.getPid())))
                .collect(Collectors.toList());

        return new Page<>(page, list);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        oaMatrixService.deleteByPid(id);

        oaMatrixClassDao.deleteById(id);
    }

    @Override
    public OaMatrixClassInfoVO info(Integer id) {
        OaMatrixClassEntity matrixClass = oaMatrixClassDao.selectById(id);
        if (matrixClass == null) {
            throw new ServiceException("分类不存在");
        }

        return new OaMatrixClassInfoVO()
                .setId(matrixClass.getId())
                .setName(matrixClass.getName())
                .setPid(matrixClass.getPid());
    }

    @Override
    public void update(OaMatrixClassUpdateDTO matrixClassUpdateDTO) {
        OaMatrixClassEntity matrixClass = oaMatrixClassDao.selectById(matrixClassUpdateDTO.getId());
        if (matrixClass == null) {
            throw new ServiceException("分类不存在");
        }

        if (Objects.equals(matrixClassUpdateDTO.getPid(), matrixClass.getId())) {
            throw new ServiceException("所属分类不能选择自己");
        }

        if (matrixClassUpdateDTO.getPid() != 0 && oaMatrixClassDao.selectById(matrixClassUpdateDTO.getPid()) == null) {
            throw new ServiceException("所属分类不存在");
        }

        OaMatrixClassEntity updateMatrixClass = new OaMatrixClassEntity()
                .setId(matrixClassUpdateDTO.getId())
                .setPid(matrixClassUpdateDTO.getPid())
                .setName(matrixClassUpdateDTO.getName());

        oaMatrixClassDao.updateById(updateMatrixClass);
    }

    @Override
    public List<SimpleObjectVO> listSimple() {
        List<OaMatrixClassEntity> matrixClassList = list();

        return matrixClassList.stream()
                .map(matrixClass -> new SimpleObjectVO()
                        .setId((long) matrixClass.getId())
                        .setName(matrixClass.getName()))
                .collect(Collectors.toList());
    }

    @Override
    public List<SimpleObjectVO> listSimpleByPid(Integer pid) {
        List<OaMatrixClassEntity> matrixClassList = oaMatrixClassDao.selectListByPid(pid);

        return matrixClassList.stream()
                .map(matrixClass -> new SimpleObjectVO()
                        .setId((long) matrixClass.getId())
                        .setName(matrixClass.getName()))
                .collect(Collectors.toList());
    }
}